import { drawMyQrcode, handlerGobackClick } from "../../../utils/util";
const app = getApp();
import User from "../../../models/user";
const user = new User();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    showError: false,
    backgroundColor: "#019cf8",
    imageUrl: "",
    flagname: "",
    phone: "",
    commid: "",
    time: "",
    visitorPhone: "",
    code: [],
    screenBrightness: "",
    options: {},
    numRefresh: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options,
    });

    wx.getScreenBrightness({
      success: (option) => {
        this.setData({
          screenBrightness: option.value,
        });
      },
    });
    //设置屏幕亮度
    wx.setScreenBrightness({
      value: 1, //屏幕亮度值，范围 0~1，0 最暗，1 最亮
    });
    this.initPageData(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });
  },
  handlerGobackClick,
  tryUp() {
    this.setData({
      showError: false,
    });
    this.refresh();
  },
  refresh() {
    this.initPageData(this.data.options);
    // this.selectComponent("#qrcode").refresh();  不要删以前的二维码刷新
  },
  onUnload() {
    wx.setScreenBrightness({
      value: this.data.screenBrightness,
    });
  },
  feedback() {
    wx.navigateTo({
      url: "/owner-app/pages/feedback/index",
    });
  },
  initPageData(options) {
    let userInfo = wx.getStorageSync("user");
    let data = {
      id: userInfo.memberId,
      commid: options.commid,
      phone: userInfo.phone,
    };

    user.miniVisitorOpenTheDoor(data).then((res) => {
      if (res.code == 1000) {
        wx.setStorageSync("qrcodeFlag", true);
        let data = res.result.qrcodeJson;
        this.setData({
          phone: data.openPhone,
          commid: data.communityId,
          time: data.time,
          visitorPhone: data.visitorPhone || "00000000000",
          code: data.code,
          flagname: "doorName",
          numRefresh: this.data.numRefresh + 1,
        });
      }
    });

    if (this.data.numRefresh >= 1) {
      this.selectComponent("#qrcode").refresh();
    }
  },
});
